package com.shiro.receiver.framework.configuration;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.shiro.receiver.framework.interceptor.WebInterceptor;

import java.util.List;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class InterceptorConfiguration implements WebMvcConfigurer {

    private final List<WebInterceptor> webInterceptorList;

    @Override
    public void addInterceptors(@NonNull InterceptorRegistry registry) {
        if (CollUtil.isEmpty(webInterceptorList)) {
            return;
        }
        for (WebInterceptor webInterceptor : webInterceptorList) {
            if (Boolean.TRUE.equals(webInterceptor.isEnable())) {
                registry.addInterceptor(webInterceptor).addPathPatterns(webInterceptor.getPathPatterns()).order(webInterceptor.getOrder());
                log.info("[拦截器注册成功:{}] [匹配路径:{}]", webInterceptor.getName(), JSONObject.toJSONString(webInterceptor.getPathPatterns()));
            }
        }
    }

}
